package com.chhuang.huaao.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.huaao.bean.HaCustomerQueryBean;
import com.chhuang.huaao.mapper.HaCustomerMapper;
import com.chhuang.huaao.po.HaCustomer;
import com.chhuang.huaao.po.HaCustomerExample;
import com.chhuang.huaao.service.IHaCustomerService;
import com.chhuang.utils.Assert;

/**
* HaCustomer ServiceImpl
* @author ChHuang
* @date Sun Apr 16 08:43:49 CST 2017
*/
@Service
public class HaCustomerServiceImpl extends BaseServiceImpl<HaCustomerMapper, HaCustomer> implements IHaCustomerService {
	@Autowired
	private HaCustomerMapper haCustomerMapper;

	@Override
	public QueryBean<HaCustomer> seniorQuery(QueryBean<HaCustomer> queryBean) throws Exception {
		HaCustomerQueryBean poQueryBean = (HaCustomerQueryBean)queryBean;
		HaCustomerExample example = new HaCustomerExample();
		
		if(poQueryBean.isAnd()){//全部是与条件
			HaCustomerExample.Criteria criteria = example.createCriteria();
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				criteria.andCustomerIdEqualTo(poQueryBean.getCustomerId());
			}
			if(Assert.isValidString(poQueryBean.getCustomerAccount())){
				criteria.andCustomerAccountLike("%"+poQueryBean.getCustomerAccount()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerPhone())){
				criteria.andCustomerPhoneLike("%"+poQueryBean.getCustomerPhone()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerPassword())){
				criteria.andCustomerPasswordLike("%"+poQueryBean.getCustomerPassword()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerName())){
				criteria.andCustomerNameLike("%"+poQueryBean.getCustomerName()+"%");
			}
			if(poQueryBean.getCustomerGender() != null){
				criteria.andCustomerGenderEqualTo(poQueryBean.getCustomerGender());
			}
			if(Assert.isValidString(poQueryBean.getCustomerCompany())){
				criteria.andCustomerCompanyLike("%"+poQueryBean.getCustomerCompany()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerPhoto())){
				criteria.andCustomerPhotoLike("%"+poQueryBean.getCustomerPhoto()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceSystem())){
				criteria.andDeviceSystemLike("%"+poQueryBean.getDeviceSystem()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceModel())){
				criteria.andDeviceModelLike("%"+poQueryBean.getDeviceModel()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceVendor())){
				criteria.andDeviceVendorLike("%"+poQueryBean.getDeviceVendor()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceUuid())){
				criteria.andDeviceUuidLike("%"+poQueryBean.getDeviceUuid()+"%");
			}
			if(Assert.isValidString(poQueryBean.getClientId())){
				criteria.andClientIdLike("%"+poQueryBean.getClientId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getClientVersion())){
				criteria.andClientVersionLike("%"+poQueryBean.getClientVersion()+"%");
			}
			if(Assert.isValidString(poQueryBean.getLastLoginAddress())){
				criteria.andLastLoginAddressLike("%"+poQueryBean.getLastLoginAddress()+"%");
			}
			if(poQueryBean.getLastLoginTime() != null){
				criteria.andLastLoginTimeEqualTo(poQueryBean.getLastLoginTime());
			}
			if(poQueryBean.getCreateTime() != null){
				criteria.andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
			if(poQueryBean.getModifyTime() != null){
				criteria.andModifyTimeEqualTo(poQueryBean.getModifyTime());
			}
		}else{//全部是或条件
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				example.or().andCustomerIdEqualTo(poQueryBean.getCustomerId());
			}
			if(Assert.isValidString(poQueryBean.getCustomerAccount())){
				example.or().andCustomerAccountLike("%"+poQueryBean.getCustomerAccount()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerPhone())){
				example.or().andCustomerPhoneLike("%"+poQueryBean.getCustomerPhone()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerPassword())){
				example.or().andCustomerPasswordLike("%"+poQueryBean.getCustomerPassword()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerName())){
				example.or().andCustomerNameLike("%"+poQueryBean.getCustomerName()+"%");
			}
			if(poQueryBean.getCustomerGender() != null){
				example.or().andCustomerGenderEqualTo(poQueryBean.getCustomerGender());
			}
			if(Assert.isValidString(poQueryBean.getCustomerCompany())){
				example.or().andCustomerCompanyLike("%"+poQueryBean.getCustomerCompany()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerPhoto())){
				example.or().andCustomerPhotoLike("%"+poQueryBean.getCustomerPhoto()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceSystem())){
				example.or().andDeviceSystemLike("%"+poQueryBean.getDeviceSystem()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceModel())){
				example.or().andDeviceModelLike("%"+poQueryBean.getDeviceModel()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceVendor())){
				example.or().andDeviceVendorLike("%"+poQueryBean.getDeviceVendor()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceUuid())){
				example.or().andDeviceUuidLike("%"+poQueryBean.getDeviceUuid()+"%");
			}
			if(Assert.isValidString(poQueryBean.getClientId())){
				example.or().andClientIdLike("%"+poQueryBean.getClientId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getClientVersion())){
				example.or().andClientVersionLike("%"+poQueryBean.getClientVersion()+"%");
			}
			if(Assert.isValidString(poQueryBean.getLastLoginAddress())){
				example.or().andLastLoginAddressLike("%"+poQueryBean.getLastLoginAddress()+"%");
			}
			if(poQueryBean.getLastLoginTime() != null){
				example.or().andLastLoginTimeEqualTo(poQueryBean.getLastLoginTime());
			}
			if(poQueryBean.getCreateTime() != null){
				example.or().andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
			if(poQueryBean.getModifyTime() != null){
				example.or().andModifyTimeEqualTo(poQueryBean.getModifyTime());
			}
		}
		
		poQueryBean.setItemCount(haCustomerMapper.countByExample(example));//总记录数
		if(poQueryBean.getItemCount()>0){//计算总页数
			poQueryBean.setPageCount(poQueryBean.getItemCount()/poQueryBean.getPageSize()+
					(int)(poQueryBean.getItemCount()%poQueryBean.getPageSize()>0?1:0));
			if(poQueryBean.getItemCount()>poQueryBean.getOffset()){
				if(Assert.isValidString(poQueryBean.getOrderBy())){//排序
					example.setOrderByClause(poQueryBean.getOrderBy()+" "+poQueryBean.getOrder());
				}
				//分页
				example.setOffset(poQueryBean.getOffset());
				example.setLimit(poQueryBean.getLimit());
				poQueryBean.setResult(haCustomerMapper.selectByExample(example));
			}
		}
		
		return poQueryBean;
	}

	@Override
	public HaCustomer findByCustomerAccount(String customerAccount) throws Exception {
		HaCustomerExample example = new HaCustomerExample();
		HaCustomerExample.Criteria criteria = example.createCriteria();
		criteria.andCustomerAccountEqualTo(customerAccount);
		List<HaCustomer> customers = haCustomerMapper.selectByExample(example);
		if(Assert.isValidCollection(customers)){
			return customers.get(0);
		}else{
			return null;
		}
	}
}